package utils

import (
	"time"

	"github.com/golang-jwt/jwt"
)

type Claims struct {
	ID int `json:"id"`
	jwt.StandardClaims
}

var jwtSecret = []byte("b0b645bac706e44d781f8bd164de58800c019a7fbeb4e7f44e173c46969ef65e")

func GetToken(id int) (string, error) {
	nowTime := time.Now()
	expireTime := nowTime.Add(3 * time.Hour)
	claims := Claims{
		id,
		jwt.StandardClaims{
			Issuer:    "100",
			ExpiresAt: expireTime.Unix(),
		},
	}

	return jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString(jwtSecret)
}

func DeToken(token string) (map[string]interface{}, bool) {
	deToken, _ := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
		return jwtSecret, nil
	})

	claims, ok := deToken.Claims.(jwt.MapClaims)
	return claims, ok
}
